﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using HYView;
using RevitInteractiveUtility.ExternalEventManager;

namespace FetchDataCompare
{
	public partial class ReportDataListForm : System.Windows.Forms.Form
	{
		public Autodesk.Revit.DB.View FirstView { get; set; }

		public Autodesk.Revit.DB.View SecondView { get; set; }

		public ExternalCommandData Revit { get; set; }

		public DocumentManger m_SrcDocM { get; set; }

		public DocumentManger m_DesDocM { get; set; }

		public List<ReportData> LstCheckReportData { get; set; }

		public List<string> LstUselessUIViewName { get; set; }

		public ReportDataListForm()
		{
			this.InitializeComponent();
		}

		public ReportDataListForm(List<ReportData> lstCheckReportData, ExternalCommandData revit, DocumentManger srcDocM, DocumentManger desDocM)
		{
			this.InitializeComponent();
			this.Revit = revit;
			this.LstCheckReportData = lstCheckReportData;
			this.m_SrcDocM = srcDocM;
			this.m_DesDocM = desDocM;
			this.LstUselessUIViewName = new List<string>();
			this.m_useEventManger = ExternalEventManagerSingleton.GetEventManger(true);
			this.m_useEventManger.AppendRequest(new DualLinkageRequest());
			this.m_useEventManger.AppendRequest(new AddBoundaryRequest());
			this.m_useEventManger.AppendRequest(new DelBoundaryRequest());
		}

		private void GetReportDataMes(ReportData reportData, ref string eleName, ref string viewName, ref string checkType)
		{
			if (reportData.SrcObjectStructBase != null)
			{
				eleName = reportData.SrcObjectStructBase.UseElement.Name;
				viewName = reportData.SrcObjectStructBase.ViewName;
			}
			else if (reportData.DesObjectStructBase != null)
			{
				eleName = reportData.DesObjectStructBase.UseElement.Name;
				viewName = reportData.DesObjectStructBase.ViewName;
			}
			checkType = reportData.CheckType;
		}

		public void DrawBoundary()
		{
			AddBoundaryBean parameters = new AddBoundaryBean
			{
				LstCheckReportData = this.LstCheckReportData,
				SrcDocM = this.m_SrcDocM,
				DesDocM = this.m_DesDocM
			};
			ExternalEventManagerSingleton.GetEventManger(false).DoRequest(1, this.Revit.Application, parameters);
		}

		private void BindData()
		{
			foreach (ReportData reportData in this.LstCheckReportData)
			{
				string value = "";
				string value2 = "";
				string value3 = "";
				this.GetReportDataMes(reportData, ref value, ref value2, ref value3);
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.Tag = reportData;
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = value;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell2.Value = value2;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				DataGridViewTextBoxCell dataGridViewTextBoxCell3 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell3.Value = value3;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell3);
				this.dgvReportData.Rows.Add(dataGridViewRow);
			}
		}

		private void GetBoundaryByReportData(ReportData reportData, ref List<Element> srcLstEle, ref List<ElementId> srcLstEleIds, ref List<Element> desLstEle, ref List<ElementId> desLstEleIds)
		{
			if (reportData == null)
			{
				return;
			}
			if (reportData.SrcObjectStructBase != null)
			{
				srcLstEle.AddRange(reportData.SrcObjectStructBase.SrcLstBoundary);
				srcLstEleIds.AddRange(reportData.SrcObjectStructBase.SrcLstBoundaryIDs);
				desLstEle.AddRange(reportData.SrcObjectStructBase.DesLstBoundary);
				desLstEleIds.AddRange(reportData.SrcObjectStructBase.DesLstBoundaryIDs);
			}
			if (reportData.DesObjectStructBase != null)
			{
				srcLstEle.AddRange(reportData.DesObjectStructBase.SrcLstBoundary);
				srcLstEleIds.AddRange(reportData.DesObjectStructBase.SrcLstBoundaryIDs);
				desLstEle.AddRange(reportData.DesObjectStructBase.DesLstBoundary);
				desLstEleIds.AddRange(reportData.DesObjectStructBase.DesLstBoundaryIDs);
			}
		}

		private void dgvReportData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			this.LstUselessUIViewName = new List<string>();
			if (e.RowIndex == -1)
			{
				return;
			}
			ReportData reportData = this.dgvReportData.Rows[e.RowIndex].Tag as ReportData;
			try
			{
				Process.GetCurrentProcess();
				List<Element> selElements = new List<Element>();
				List<ElementId> list = new List<ElementId>();
				List<Element> selElements2 = new List<Element>();
				List<ElementId> list2 = new List<ElementId>();
				this.GetBoundaryByReportData(reportData, ref selElements, ref list, ref selElements2, ref list2);
				Autodesk.Revit.DB.View view = null;
				Autodesk.Revit.DB.View view2 = null;
				string viewNameUniqueID = "";
				if (reportData.SrcObjectStructBase != null)
				{
					viewNameUniqueID = reportData.SrcObjectStructBase.UseView.UniqueId;
				}
				else
				{
					viewNameUniqueID = reportData.DesObjectStructBase.UseView.UniqueId;
				}
				UIDocument uidocument = this.Revit.Application.OpenAndActivateDocument(reportData.SrcPathName);
				this.GetDocumentView(uidocument.Document, viewNameUniqueID, ref view);
				this.FirstView = view;
				uidocument.ActiveView = view;
				foreach (UIView uiview in uidocument.GetOpenUIViews())
				{
					if (uiview.ViewId == this.FirstView.Id)
					{
						this.m_FirstUiView = uiview;
					}
					else
					{
						Autodesk.Revit.DB.View view3 = uidocument.Document.GetElement(uiview.ViewId) as Autodesk.Revit.DB.View;
						if (view3 != null && !this.LstUselessUIViewName.Contains(view3.Title))
						{
							this.LstUselessUIViewName.Add(view3.Title);
						}
					}
				}
				try
				{
					uidocument.ShowElements(list);
					RevitVersionFuncs.SetSelection(uidocument.Selection, selElements);
					uidocument.RefreshActiveView();
				}
				catch (Exception)
				{
					this.m_FirstUiView.Zoom(1.1);
				}
				UIDocument uidocument2 = this.Revit.Application.OpenAndActivateDocument(reportData.DesPathName);
				this.GetDocumentView(uidocument2.Document, viewNameUniqueID, ref view2);
				this.SecondView = view2;
				uidocument2.ActiveView = view2;
				foreach (UIView uiview2 in uidocument2.GetOpenUIViews())
				{
					if (uiview2.ViewId == this.SecondView.Id)
					{
						this.m_SecondUiView = uiview2;
					}
					else
					{
						Autodesk.Revit.DB.View view4 = uidocument.Document.GetElement(uiview2.ViewId) as Autodesk.Revit.DB.View;
						if (view4 != null && !this.LstUselessUIViewName.Contains(view4.Title))
						{
							this.LstUselessUIViewName.Add(view4.Title);
						}
					}
				}
				try
				{
					uidocument2.ShowElements(list2);
					RevitVersionFuncs.SetSelection(uidocument2.Selection, selElements2);
					uidocument2.RefreshActiveView();
				}
				catch (Exception)
				{
					this.m_SecondUiView.Zoom(1.1);
				}
				new TileView(this.Revit, view.Title, view2.Title, this.LstUselessUIViewName).TileViewByViewName();
			}
			catch (Exception)
			{
				MessageBox.Show("视图同步对比操作不支持非盈建科模板", "盈建科提示");
			}
		}

		private void GetDocumentView(Autodesk.Revit.DB.Document doc, string viewNameUniqueID, ref Autodesk.Revit.DB.View view)
		{
            //ReportDataListForm.<>c__DisplayClass41_0 <>c__DisplayClass41_ = new ReportDataListForm.<>c__DisplayClass41_0();
            //FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
            //ReportDataListForm.<>c__DisplayClass41_0 <>c__DisplayClass41_2 = <>c__DisplayClass41_;
            //ViewType[] array = new ViewType[6];
            //RuntimeHelpers.InitializeArray(array, fieldof(<PrivateImplementationDetails>.464E21EF5C9C38639C7920E6E81D4B73F3EE7A35).FieldHandle);
            //<>c__DisplayClass41_2.viewTypes = array;
            //foreach (Element element in from Autodesk.Revit.DB.View item in filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.View)).WhereElementIsNotElementType()
            //where !item.IsTemplate && <>c__DisplayClass41_.viewTypes.Contains(item.ViewType)
            //select item)
            //{
            //    Autodesk.Revit.DB.View view2 = element as Autodesk.Revit.DB.View;
            //    if (view2 != null && view2.UniqueId == viewNameUniqueID)
            //    {
            //        view = view2;
            //    }
            //}
		}

		private void ReportDataListForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.BindData();
			Process currentProcess = Process.GetCurrentProcess();
			this.m_RevitMainHandle = currentProcess.MainWindowHandle;
			this.m_hWndViewProcNew = new ReportDataListForm.NewWndProc(this.WndMsgProc);
			this.m_hWndViewProcOld = ReportDataListForm.SetWindowLong(this.m_RevitMainHandle, -4, this.m_hWndViewProcNew);
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 27;
			if (m.WParam.ToInt32() == num)
			{
				base.Close();
			}
			return false;
		}

		private void ReportDataListForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			IntPtr hWndViewProcOld = this.m_hWndViewProcOld;
			ReportDataListForm.SetWindowLong(this.m_RevitMainHandle, -4, this.m_hWndViewProcOld);
			DelBoundaryBean delBoundaryBean = new DelBoundaryBean();
			delBoundaryBean.UseForm = this;
			ExternalEventManagerSingleton.GetEventManger(false).DoRequest(2, this.Revit.Application, delBoundaryBean);
		}

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, ReportDataListForm.NewWndProc wndproc);

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

		public IntPtr WndMsgProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
		{
			IntPtr result;
			try
			{
				IntPtr intPtr = ReportDataListForm.CallWindowProc(this.m_hWndViewProcOld, hWnd, msg, wParam, lParam);
				if (msg != 27)
				{
					if (msg != 32)
					{
						if (msg != 522)
						{
						}
					}
					else
					{
						UIDocument activeUIDocument = this.Revit.Application.ActiveUIDocument;
						DualLinkageBean parameters = new DualLinkageBean
						{
							UiDoc = activeUIDocument,
							FirstView = this.FirstView,
							FirstUIView = this.m_FirstUiView,
							SecondView = this.SecondView,
							SecondUIView = this.m_SecondUiView
						};
						ExternalEventManagerSingleton.GetEventManger(false).DoRequest(0, this.Revit.Application, parameters);
					}
				}
				else
				{
					base.Close();
				}
				result = intPtr;
			}
			catch (Exception)
			{
				result = IntPtr.Zero;
			}
			return result;
		}

		private ReportDataListForm.NewWndProc m_hWndViewProcNew;

		private IntPtr m_hWndViewProcOld = IntPtr.Zero;

		private IntPtr m_RevitMainHandle = IntPtr.Zero;

		private UIView m_FirstUiView;

		private UIView m_SecondUiView;

		private ExternalEventManagerSingleton m_useEventManger;

		public delegate IntPtr NewWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
	}
}
